import { BasicTable, useTable, BasicColumn, FormSchema, useRender } from "@/components/Table";
import { Tag, message } from "antd";
import StockInForm from "./StockInForm";
import { useDrawer } from "@/components/Drawer";
import { StockInApi } from "@/api/erp/stock/in";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { SupplierApi } from "@/api/erp/purchase/supplier";
import * as UserApi from "@/api/system/user";
import { exportToExcel } from "@/utils/util";
import { useTranslation } from "react-i18next";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { IconEnum } from "@/enums/appEnum";
import TableAction from "@/components/Table/components/TableAction";
const ErpStockIn = () => {
	const { t } = useTranslation();
	const [registerDrawer, { openDrawer }] = useDrawer();
	const en_vi = "drugStorage.inventoryManagement";
	// 配置table
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.otherInboundItems.invtInId`),
			dataIndex: "invtInId"
		},
		{
			title: t(`drugStorage.productName`),
			dataIndex: "productNames"
		},
		{
			title: t(`${en_vi}.otherInboundItems.suplerName`),
			dataIndex: "suplerName"
		},
		{
			title: t("felix_ui.createdTime"),
			dataIndex: "createdTime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.inventoryDetails.createdBy`),
			dataIndex: "createdByName"
		},
		{
			title: t(`${en_vi}.inventoryDetails.totalCount`),
			dataIndex: "totalCount",
			render: text => parseFloat(text).toFixed(3)
		},
		{
			title: t(`${en_vi}.otherInboundItems.totalPrice`),
			dataIndex: "totalPrice",
			render: text => parseFloat(text).toFixed(2)
		},
		{
			title: t(`${en_vi}.warehouseInformation.status`),
			dataIndex: "status",
			render: (_, { status }) => (
				<>
					{status == "20" ? (
						<Tag color="success">{t(`${en_vi}.otherInboundItems.audited`)}</Tag>
					) : (
						<Tag color="error">{t(`${en_vi}.otherInboundItems.unaudited`)}</Tag>
					)}
				</>
			)
		},
		{
			title: t("felix_ui.action"),
			dataIndex: "action",
			fixed: "right",
			render: (_: any, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: handleEdit.bind(null, record)
						},
						{
							icon: IconEnum.DELETE,
							danger: true,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("felix_ui.delMessage"),
								placement: "top",
								confirm: handleDelete.bind(null, [record.invtInId])
							}
						}
					]}
				></TableAction>
			)
		}
	];
	const formSchemaTop: FormSchema[] = [
		{
			label: t(`${en_vi}.otherInboundItems.invtInId`),
			component: "Input",
			field: "invtInId"
		},
		{
			label: t(`${en_vi}.otherInboundItems.suplerName`),
			component: "ApiSelect",
			field: "suplerId",
			componentProps: {
				api: SupplierApi.getSupplierSimpleList,
				labelField: "name",
				valueField: "suplerId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.drugInventory.warehouseName`),
			component: "ApiSelect",
			field: "whsId",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList,
				labelField: "name",
				valueField: "whsId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.inventoryDetails.createdBy`),
			component: "ApiSelect",
			field: "createdBy",
			componentProps: {
				api: UserApi.getListSimpleUsers,
				labelField: "nickname",
				valueField: "id",
				immediate: false
			}
		},
		{
			label: t("felix_ui.status"),
			component: "Select",
			field: "status",
			componentProps: {
				options: [
					{ label: t(`${en_vi}.otherInboundItems.audited`), value: "20" },
					{ label: t(`${en_vi}.otherInboundItems.unaudited`), value: "10" }
				]
			}
		}
	];
	// 配置table
	const [registerTable, { reload }] = useTable({
		api: StockInApi.getStockInPage,
		columns,
		formConfig: {
			labelWidth: 150,
			schemas: formSchemaTop,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 },
			actionColOptions: { span: 24 }
		},
		bordered: true,
		useSearchForm: true,
		searchInfo: {
			bizType: "1"
		}
	});
	//编辑弹窗
	const handleEdit = (record: any) => {
		openDrawer(true, {
			reload,
			record,
			isUpdated: true
		});
	};
	//删除
	const handleDelete = async (ids: number[]) => {
		// 发起删除
		await StockInApi.deleteStockIn(ids);
		reload();
		message.success(t("kevin_ui.daleteSucceessfully"));
	};
	//表格导出
	const exportExcel = async () => {
		await StockInApi.getStockInPage({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload,
					isUpdated: false
				});
				break;
			case "export":
				exportExcel();
				break;
		}
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
				<StockInForm register={registerDrawer}></StockInForm>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={["add", "export"]} />
		</div>
	);
};
export default ErpStockIn;
